/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import Logica.DiaLaboral;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logica.Taller;
import Logica.Turno;
import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Pablo
 */
public class DiaLaboralJpaController implements Serializable {
    
    public DiaLaboralJpaController(){
        emf = Persistence.createEntityManagerFactory("TpFinalPU");
    }


    public DiaLaboralJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(DiaLaboral diaLaboral) throws PreexistingEntityException, Exception {
        if (diaLaboral.getTurnos() == null) {
            diaLaboral.setTurnos(new ArrayList<Turno>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Taller unTaller = diaLaboral.getUnTaller();
            if (unTaller != null) {
                unTaller = em.getReference(unTaller.getClass(), unTaller.getNroTaller());
                diaLaboral.setUnTaller(unTaller);
            }
            List<Turno> attachedTurnos = new ArrayList<Turno>();
            for (Turno turnosTurnoToAttach : diaLaboral.getTurnos()) {
                turnosTurnoToAttach = em.getReference(turnosTurnoToAttach.getClass(), turnosTurnoToAttach.getNroTurno());
                attachedTurnos.add(turnosTurnoToAttach);
            }
            diaLaboral.setTurnos(attachedTurnos);
            em.persist(diaLaboral);
            if (unTaller != null) {
                unTaller.getDiasLaborables().add(diaLaboral);
                unTaller = em.merge(unTaller);
            }
            for (Turno turnosTurno : diaLaboral.getTurnos()) {
                DiaLaboral oldUnDiaLaboralOfTurnosTurno = turnosTurno.getUnDiaLaboral();
                turnosTurno.setUnDiaLaboral(diaLaboral);
                turnosTurno = em.merge(turnosTurno);
                if (oldUnDiaLaboralOfTurnosTurno != null) {
                    oldUnDiaLaboralOfTurnosTurno.getTurnos().remove(turnosTurno);
                    oldUnDiaLaboralOfTurnosTurno = em.merge(oldUnDiaLaboralOfTurnosTurno);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findDiaLaboral(diaLaboral.getFecha()) != null) {
                throw new PreexistingEntityException("DiaLaboral " + diaLaboral + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(DiaLaboral diaLaboral) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DiaLaboral persistentDiaLaboral = em.find(DiaLaboral.class, diaLaboral.getFecha());
            Taller unTallerOld = persistentDiaLaboral.getUnTaller();
            Taller unTallerNew = diaLaboral.getUnTaller();
            List<Turno> turnosOld = persistentDiaLaboral.getTurnos();
            List<Turno> turnosNew = diaLaboral.getTurnos();
            if (unTallerNew != null) {
                unTallerNew = em.getReference(unTallerNew.getClass(), unTallerNew.getNroTaller());
                diaLaboral.setUnTaller(unTallerNew);
            }
            List<Turno> attachedTurnosNew = new ArrayList<Turno>();
            for (Turno turnosNewTurnoToAttach : turnosNew) {
                turnosNewTurnoToAttach = em.getReference(turnosNewTurnoToAttach.getClass(), turnosNewTurnoToAttach.getNroTurno());
                attachedTurnosNew.add(turnosNewTurnoToAttach);
            }
            turnosNew = attachedTurnosNew;
            diaLaboral.setTurnos(turnosNew);
            diaLaboral = em.merge(diaLaboral);
            if (unTallerOld != null && !unTallerOld.equals(unTallerNew)) {
                unTallerOld.getDiasLaborables().remove(diaLaboral);
                unTallerOld = em.merge(unTallerOld);
            }
            if (unTallerNew != null && !unTallerNew.equals(unTallerOld)) {
                unTallerNew.getDiasLaborables().add(diaLaboral);
                unTallerNew = em.merge(unTallerNew);
            }
            for (Turno turnosOldTurno : turnosOld) {
                if (!turnosNew.contains(turnosOldTurno)) {
                    turnosOldTurno.setUnDiaLaboral(null);
                    turnosOldTurno = em.merge(turnosOldTurno);
                }
            }
            for (Turno turnosNewTurno : turnosNew) {
                if (!turnosOld.contains(turnosNewTurno)) {
                    DiaLaboral oldUnDiaLaboralOfTurnosNewTurno = turnosNewTurno.getUnDiaLaboral();
                    turnosNewTurno.setUnDiaLaboral(diaLaboral);
                    turnosNewTurno = em.merge(turnosNewTurno);
                    if (oldUnDiaLaboralOfTurnosNewTurno != null && !oldUnDiaLaboralOfTurnosNewTurno.equals(diaLaboral)) {
                        oldUnDiaLaboralOfTurnosNewTurno.getTurnos().remove(turnosNewTurno);
                        oldUnDiaLaboralOfTurnosNewTurno = em.merge(oldUnDiaLaboralOfTurnosNewTurno);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Calendar id = diaLaboral.getFecha();
                if (findDiaLaboral(id) == null) {
                    throw new NonexistentEntityException("The diaLaboral with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Calendar id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            DiaLaboral diaLaboral;
            try {
                diaLaboral = em.getReference(DiaLaboral.class, id);
                diaLaboral.getFecha();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The diaLaboral with id " + id + " no longer exists.", enfe);
            }
            Taller unTaller = diaLaboral.getUnTaller();
            if (unTaller != null) {
                unTaller.getDiasLaborables().remove(diaLaboral);
                unTaller = em.merge(unTaller);
            }
            List<Turno> turnos = diaLaboral.getTurnos();
            for (Turno turnosTurno : turnos) {
                turnosTurno.setUnDiaLaboral(null);
                turnosTurno = em.merge(turnosTurno);
            }
            em.remove(diaLaboral);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<DiaLaboral> findDiaLaboralEntities() {
        return findDiaLaboralEntities(true, -1, -1);
    }

    public List<DiaLaboral> findDiaLaboralEntities(int maxResults, int firstResult) {
        return findDiaLaboralEntities(false, maxResults, firstResult);
    }

    private List<DiaLaboral> findDiaLaboralEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(DiaLaboral.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public DiaLaboral findDiaLaboral(Calendar id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(DiaLaboral.class, id);
        } finally {
            em.close();
        }
    }

    public int getDiaLaboralCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<DiaLaboral> rt = cq.from(DiaLaboral.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
